package com.whity.towerdefense.view;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

import javax.microedition.khronos.opengles.GL10;

import android.graphics.PointF;

public class GraphicDebugElt implements GraphicDrawable
{
	// =============================================================
	// Private attributs :
	
	@SuppressWarnings("unused")
	private static final String			TAG = "com.djm.test03.view";
	
	private FloatBuffer 				vertexBuffer;
	private ShortBuffer 				indexBuffer;
	
	protected PointF 					m_Position;
	protected float 					m_ScaleX;
	protected float						m_ScaleY;
	protected float						m_Width;
	protected float						m_Height;
	
	//private float
	
	// =============================================================
	// Public functions :
	
	/**
	 * Constructor to build a rectangle
	 * @param p_Width Width of the rectangle
	 * @param p_Height Height of the rectangle
	 */
	public GraphicDebugElt(float p_Width, float p_Height)
	{
		m_Position = new PointF(0.0f, 0.0f);
		m_ScaleX = 1.0f;
		m_ScaleY = 1.0f;
		m_Width = p_Width;
		m_Height =  p_Height;
		
		float vertices[] =
			{
		       0.0f,  m_Height, 0.0f,  // 0, Top Left
		       0.0f,  0.0f, 0.0f,  // 1, Bottom Left
		       m_Width,  0.0f, 0.0f,  // 2, Bottom Right
		       m_Width,  m_Height, 0.0f,  // 3, Top Right
			};

		ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
		vbb.order(ByteOrder.nativeOrder());
		vertexBuffer = vbb.asFloatBuffer();
		vertexBuffer.put(vertices);
		vertexBuffer.position(0);
		
		short[] indices = { 0, 1, 2, 3 };
		ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2);
		ibb.order(ByteOrder.nativeOrder());
		indexBuffer = ibb.asShortBuffer();
		indexBuffer.put(indices);
		indexBuffer.position(0);
	}
	
	/**
	 * Constructor to build a grid
	 * @param p_WidthInCell the width of the grid in cell
	 * @param p_HeightInCell the height of the grid in cell
	 * @param p_CellWidth width of a cell
	 * @param p_CellHeight height of a cell
	 */
	public GraphicDebugElt(int p_WidthInCell, int p_HeightInCell, float p_CellWidth, float p_CellHeight)
	{
		m_Position = new PointF(0.0f, 0.0f);
		m_ScaleX = 1.0f;
		m_ScaleY = 1.0f;
		m_Width = p_WidthInCell*p_CellWidth;
		m_Height =  p_HeightInCell*p_CellHeight;
		
		float[] vertices = new float[3*(p_WidthInCell+1)*(p_HeightInCell+1)];
		for(int j = 0 ; j <= p_HeightInCell ; j++)
		{
			for(int i = 0 ; i <= p_WidthInCell ; i++)
			{
				vertices[3*i+3*j*(p_WidthInCell+1) + 0] = i*p_CellWidth; 	// x
				vertices[3*i+3*j*(p_WidthInCell+1) + 1] = j*p_CellHeight; 	// y
				vertices[3*i+3*j*(p_WidthInCell+1) + 2] = 0.0f; 			// z
			}
		}

		ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
		vbb.order(ByteOrder.nativeOrder());
		vertexBuffer = vbb.asFloatBuffer();
		vertexBuffer.put(vertices);
		vertexBuffer.position(0);
		
		short[] indices = new short[(p_WidthInCell+1)*(p_HeightInCell+1)*2];
		for(int j = 0 ; j <= p_HeightInCell ; j++)
		{
			indices[j*2 + 0] = (short) (j*(p_WidthInCell+1));
			indices[j*2 + 1] = (short) (j*(p_WidthInCell+1)+p_WidthInCell);
		}
		
		for(int i = 0 ; i <= p_WidthInCell ; i++)
		{
			indices[(p_HeightInCell+1)*2 + i*2 + 0] = (short) i;
			indices[(p_HeightInCell+1)*2 + i*2 + 1] = (short) (i + (p_WidthInCell+1)*p_HeightInCell);
		}
		
		
		ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2);
		ibb.order(ByteOrder.nativeOrder());
		indexBuffer = ibb.asShortBuffer();
		indexBuffer.put(indices);
		indexBuffer.position(0);
	}
	
	public void Draw(GL10 p_GL)
	{
		p_GL.glPushMatrix();
		
			// Position and scale
			p_GL.glTranslatef(m_Position.x, m_Position.y, 0.0f);
			p_GL.glScalef(m_ScaleX, m_ScaleY, 1.0f);
			
		
			// Enabled the vertices buffer for writing and to be used during rendering.
			p_GL.glEnableClientState(GL10.GL_VERTEX_ARRAY);
			
				// Specifies the location and data format of an array of vertex coordinates to use when rendering.
				p_GL.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
				
				p_GL.glDrawElements(GL10.GL_LINES, indexBuffer.capacity(), GL10.GL_UNSIGNED_SHORT, indexBuffer);
		
			// Disable the vertices buffer.
			p_GL.glDisableClientState(GL10.GL_VERTEX_ARRAY);
			
		
		p_GL.glPopMatrix();
	}
	
	public PointF GetPosition()
	{
		return m_Position;
	}
	
	public void SetPosition(float p_X, float  p_Y)
	{
		m_Position.set(p_X, p_Y);
	}
	
	public float GetScale()
	{
		if(m_ScaleX == m_ScaleY)
			return m_ScaleX;
		else
			return -1;
	}
	
	public void SetScale(float p_Scale)
	{
		m_ScaleX = m_ScaleY = p_Scale;
	}
	
	public float GetWidth()
	{
		return m_Width;
	}
	
	public float GetHeight()
	{
		return m_Height;
	}
		
		// =============================================================
}
